<?php
/**
 * User: aka
 * Date: 2018/6/25
 * Time: 14:22
 */

namespace app\backstage\model;


use app\common\db\DbOperation;

class Manage
{
    static private $enterprisePhone = 'enterprise_phone';
    static private $enterpriseAddress = 'enterprise_address';
    static private $enterpriseInfo = 'enterprise_info';
    static private $sowingImg = 'sowing_img';

    public $dbOperation;

    public function __construct()
    {
        $this->dbOperation = new DbOperation();
    }

    public function __destruct()
    {
        unset($this->dbOperation);
    }

    private function getDbOperation() : DbOperation
    {
        return $this->dbOperation;
    }

    public function enterprisePhoneAction(array $requestData) : bool
    {
        $updateData = [
            'seat_number'=>$requestData['seat_number'],
            'cell_phone_number'=>$requestData['cell_phone_number'],
            'private_number'=>$requestData['private_number']
        ];
        return $this->getDbOperation()->update(self::$enterprisePhone,1,$updateData);
    }

    public function contactAddressAction(array $requestData) : bool
    {
        $updateData = [
            'enterprise_name'=>$requestData['enterprise_name'],
            'enterprise_address'=>$requestData['enterprise_address'],
            'enterprise_proposal'=>$requestData['enterprise_proposal']
        ];
        return $this->getDbOperation()->update(self::$enterpriseAddress,1,$updateData);
    }

    private function updateEnterpriseInfo(array $ArrayData) : bool
    {
        $result = $this->getDbOperation()->update(self::$enterpriseInfo,1,$ArrayData);
        return $result ? true : false;
    }

    public function enterpriseCooperationAction(array $requestData) : bool
    {
        return $this->updateEnterpriseInfo(['enterprise_cooperation'=>$requestData['enterprise_cooperation']]);
    }

    public function enterpriseRecoveryAction(array $requestData) : bool
    {
        return $this->updateEnterpriseInfo(['enterprise_recovery'=>$requestData['enterprise_recovery']]);
    }

    public function aboutUsAction(array $requestData) : bool
    {
        return $this->updateEnterpriseInfo(['about_us'=>$requestData['about_us']]);
    }

    public function processFlowAction(array $requestData) : bool
    {
        return $this->updateEnterpriseInfo(['process_flow'=>$requestData['process_flow']]);
    }

    public function mattersNeedingAttentionAction(array $requestData) : bool
    {
        return $this->updateEnterpriseInfo(['matters_needing_attention'=>$requestData['matters_needing_attention']]);
    }

    public function exemptionFromDepositAction(array $requestData) : bool
    {
        return $this->updateEnterpriseInfo(['exemption_from_deposit'=>$requestData['exemption_from_deposit']]);
    }

    public function ercodeUploadAction(string $singleImgUrl) : bool
    {
        return $this->updateEnterpriseInfo(['ercode'=>$singleImgUrl]);
    }

    private function DisableOperation(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$sowingImg,$requestData['id'],['status' => 0]);
    }

    private function EnableOperation(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$sowingImg,$requestData['id'],['status' => 1]);
    }

    public function statusOperationAction(array $requestData) : int
    {
        $findImgData = $this->getDbOperation()->find(self::$sowingImg,['id'=>$requestData['id']]);
        if ($findImgData['status'] == 1)
        {
            $result = $this->DisableOperation($requestData);
            return !$result?:0;
        }else{
            $result = $this->EnableOperation($requestData);
            return !$result?:1;
        }
    }

    public function sowingImgUploadAction(string $singleImgUrl) : bool
    {
        return $this->getDbOperation()->add(self::$sowingImg,['sowing_img'=>$singleImgUrl]);
    }

    public function sowingImgEditAction(string $id,string $singleImgUrl) : bool
    {
        return $this->getDbOperation()->update(self::$sowingImg,$id,['sowing_img'=>$singleImgUrl]);
    }
}